using UnityEngine;
using System.Collections;

public class MenuOptions : Menu 
{
	private bool isPause;
	private bool tutorial;

    public newMenu target;

	public UI_TextMessage soundOn;

    private bool first;

	void Start()
	{		
		base._manager = gameObject.GetComponent<MenuManager>();
		base.amount = base._manager.amount;
	
		beginNavigationY(limit,yieldReturnControls);
				
		effectSwitch(0);

	}
	
	void Awake()
	{
		ChangeStringId(soundOn,PlayerPrefs.GetString("Audio"),isPause,"Audio");
	}
	
	void Update()
	{
		
		if(ready && Input.GetAxisRaw("Vertical") != 0)
		{
			base.navigationY();
			effectSwitch(this.actualNavigationPosY);
		}
		
		if(ready && base.hasPressEnter())
		{
			this.executeAction(base.actualNavigationPosY);
		}	
	}
	
	public override void effectSwitch(int number)
	{
		if(number != lastNumberY)
		{
			this.effectSwitchFlashColors(this.actualNavigationPosY,this.lastNumberY);
		}
		else if(!ready)
		{
			this.effectSwitchFlashColors(this.actualNavigationPosY,this.lastNumberY);
		}
	}

    void Resume()
    {
        this.actualNavigationPosY = 0;

        if (!first)
        {
            first = true;
        }
        else
        {
            this.effectSwitchFlashColors(this.actualNavigationPosY, 4);
        }

    }

	public override void executeAction(int number)
	{
	
		if(number == 0)
		{
			if(isPause)
			{
				PlayerPrefs.SetString("Audio","Off");
				ChangeStringId(soundOn,"Off",isPause,"Audio");
				AudioListener.volume = 0;
			}
			else
			{
				PlayerPrefs.SetString("Audio","On");
				ChangeStringId(soundOn,"On",isPause,"Audio");
				AudioListener.volume = 1;
			}
		}
		if(number == 1)
		{
			base._manager.changeScene(menuElements[number].name);
		}
		
		if(number == 2)
		{
			base._manager.changeScene(menuElements[number].name);
		}
		
		if(number == 3)
		{
            target.Back();
		}
		
	}
	
	private void ChangeStringId(UI_TextMessage message,string state,bool changeState,string fromName)
	{
		if(state.Equals("On"))
		{
			message.id = "menu_options_confirmation";
			changeState =  true;
		}
		else
		{
			message.id = "menu_options_notconfirmation";
			changeState = false;
		}
		
		switch(fromName)
		{
			case "Audio":
				isPause = changeState;
				break;
			
			case "Tutorial":
				tutorial = changeState;
				break;
		}

		message.reloadText();
	}
	
	void Ready()
	{
		base.ready = true;
	}
	
}


